const Joi = require("joi");
const response = require("../utils/response");
// 定义验证模式
//上传文件接口
const addFile_schema = Joi.object({
  remark: Joi.string().required(),
  fileType: Joi.number().required(),
  spaceId: Joi.string().required(),
  file: Joi.object({
    fieldname: Joi.string().required(),
    originalname: Joi.string().required(),
    encoding: Joi.string().required(),
    mimetype: Joi.string()
      .valid("image/jpeg", "image/png", "image/gif","image/webp")
      .required(),
    size: Joi.number()
      .max(5 * 1024 * 1024)
      .required(), // 限制文件大小为 5MB
  })
    .unknown(true)
    .required(),
});

// 验证中间件
const validate = (schema) => {
  return (req, res, next) => {
    const { error } = schema.validate({
      ...req.body,
      file: req.file,
    });
    if (error) {
      return res.json(response.error(error.details[0].message,null));
    }
    next();
  };
};
module.exports = {
  validate,
  addFile_schema,
};
